package com.hairline.hairlinemarket.service.impl;

import com.hairline.hairlinemarket.bean.MarketCart;
import com.hairline.hairlinemarket.bean.MarketGoodsProduct;
import com.hairline.hairlinemarket.bean.MarketGoodsProductExample;
import com.hairline.hairlinemarket.mapper.MarketGoodsProductMapper;
import com.hairline.hairlinemarket.service.CartService;
import com.hairline.hairlinemarket.service.CategoryService;
import com.hairline.hairlinemarket.service.GoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:杨小凯
 * Date:2023-04-21 7:30
 * Description:<描述>
 */
@Service
public class GoodsProductServiceImpl implements GoodsProductService {
    @Autowired
    private MarketGoodsProductMapper goodsProductMapper;
    @Autowired
    private CartService cartService;

    @Override
    public List queryByGoodsId(Integer id) {

        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria = goodsProductExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        return marketGoodsProducts;
    }

    @Override
    public boolean updateStock(Integer userId,Integer goodsId, Integer number, Integer productId) {
        // 查询规格
        MarketCart marketCart = cartService.selectSpe(userId,productId);
        String specifications = marketCart.getSpecifications();
        // 根据goodsID和specification
        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria exampleCriteria = goodsProductExample.createCriteria();
        if (null==specifications){
            exampleCriteria.andGoodsIdEqualTo(goodsId);
        }else {
            exampleCriteria.andGoodsIdEqualTo(goodsId).andSpecificationsEqualTo(specifications);
        }
        List<MarketGoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        if(goodsProducts!=null&&goodsProducts.size()>0){
            MarketGoodsProduct marketGoodsProduct = goodsProducts.get(0);
            number = marketGoodsProduct.getNumber() - number;
            if (number>=0){
                // 更新数据
                MarketGoodsProduct goodsProduct = new MarketGoodsProduct();
                goodsProduct.setNumber(number);
                goodsProductMapper.updateByExampleSelective(goodsProduct, goodsProductExample);
                return true;
            }
        }
        return false;
    }

    @Override
    public void updateAddStock(Integer userId, List<Integer> productIds) {
        // 查询购物车中对应的订单
        List<MarketCart> cartList = cartService.selectByUPs(userId,productIds);

        for (MarketCart marketCart : cartList) {
            Integer productId = marketCart.getProductId();
            // 更新库存

            MarketGoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(productId);

            MarketGoodsProduct record = new MarketGoodsProduct();
            record.setNumber(marketCart.getNumber()+goodsProduct.getNumber());
            MarketGoodsProductExample example = new MarketGoodsProductExample();
            example.createCriteria().andIdEqualTo(productId);
            goodsProductMapper.updateByExampleSelective(record, example);
        }
    }
}
